package com.ckd.flyingtrip.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.activity.CarDetailActivity;
import com.ckd.flyingtrip.activity.HomeIndulgenceActivity;
import com.ckd.flyingtrip.activity.HomeNewsActivity;
import com.ckd.flyingtrip.activity.HomeNewsDetailActivity;
import com.ckd.flyingtrip.activity.HomePayActivity;
import com.ckd.flyingtrip.activity.HomePopularActivity;
import com.ckd.flyingtrip.activity.MainActivity;
import com.ckd.flyingtrip.activity.NameRenzhengActivity;
import com.ckd.flyingtrip.activity.OrderActity;
import com.ckd.flyingtrip.adapter.DialogAdapter;
import com.ckd.flyingtrip.adapter.HomeAdapter;
import com.ckd.flyingtrip.adapter.HomeAdapter1;
import com.ckd.flyingtrip.charge.activity.MapActivity;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.HomeGetnoticeBean;
import com.ckd.flyingtrip.javabean.HomeIndulgenceBean;
import com.ckd.flyingtrip.javabean.HomePopularBean;
import com.ckd.flyingtrip.shared.activity.SharedCarActivity;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.CustomDialog;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.view.UPMarqueeView;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeFragment extends Fragment {

    @BindView(R.id.fragment_home_UPMarqueeView)
    UPMarqueeView upMarqueeView;
    @BindView(R.id.fragment_home_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.fragment_home_recyclerView1)
    RecyclerView recyclerView1;

    List<View> views = new ArrayList<>();
    @BindView(R.id.fragment_home_weather_icon)
    ImageView weatherIcon;
    @BindView(R.id.fragment_home_weather_textView)
    TextView weatherTextView;
    @BindView(R.id.fragment_home_icon)
    ImageView icon;
    @BindView(R.id.fragment_home_realName)
    LinearLayout realName;

    private HomeAdapter adapter;
    private List<HomePopularBean.DataBean.ListBean> dataBeans1 = new ArrayList<>();
    private HomeAdapter1 adapter1;
    private List<HomeIndulgenceBean.DataBean.ListBean> listBeans = new ArrayList<>();

    private List<HomeGetnoticeBean.DataBean> dataBeans = new ArrayList<>();

    public static int userStart = 2;

    private CustomDialog.Builder builder, builder1, builder2, builder3;
    private CustomDialog dialog, dialog1, dialog2, dialog3;

    private String batteryVoltage = "";
    private String sweepMark = "0";
    private String battry = "";
    private String powerChangeMark = "";

    private DialogAdapter dialogAdapter;
    private List<String> strings = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        ButterKnife.bind(this, view);

        initView();
        initData();
        initListener();

        return view;
    }

    private void initListener() {
        adapter1.setOnItemClickListener(new HomeAdapter1.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Intent inten = new Intent(getActivity(), GoumaiActivity.class);
                Intent inten = new Intent(getActivity(), CarDetailActivity.class);
                inten.putExtra("id", listBeans.get(position).getBicycleId());
                startActivity(inten);
            }
        });

        adapter.setOnItemClickListener(new HomeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
//                Intent inten = new Intent(getActivity(), GoumaiActivity.class);
                Intent inten = new Intent(getActivity(), CarDetailActivity.class);
                inten.putExtra("id", dataBeans1.get(position).getBicycleId());
                startActivity(inten);
            }
        });

        upMarqueeView.setOnItemClickListener(new UPMarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view) {
                Intent intent = new Intent(getActivity(), HomeNewsDetailActivity.class);
                intent.putExtra("title", dataBeans.get(position).getNoticeTitle());
                intent.putExtra("text", dataBeans.get(position).getNoticeMsg());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        initWeather();
        initNews();
//        initIcon();
        initPopular();
        initIndulgence();
    }

    private void initIcon() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.gethomepicture, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("图片", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("图片", "" + volleyError);
            }
        });
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    private void initIndulgence() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.fiveoffbicycleinfo, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("特惠", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        HomeIndulgenceBean bean = new Gson().fromJson(s, HomeIndulgenceBean.class);
                        listBeans.clear();
                        listBeans.addAll(bean.getData().getList());
                        if (adapter1 != null)
                            adapter1.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("特惠", "" + volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userArea", Constants.CITYID);
                    json_url.put("page", 1);
                    json_url.put("row", 2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    private void initPopular() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.hotrentcarinfo, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("热门租车", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        HomePopularBean bean = new Gson().fromJson(s, HomePopularBean.class);
                        dataBeans1.clear();
                        dataBeans1.addAll(bean.getData().getList());
                        if (adapter != null)
                            adapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("热门租车", "" + volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userArea", Constants.CITYID);
                    json_url.put("page", 1);
                    json_url.put("row", 3);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    private void initNews() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.getnotice, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("通知公告", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        HomeGetnoticeBean bean = new Gson().fromJson(s, HomeGetnoticeBean.class);
                        dataBeans.clear();
                        dataBeans.addAll(bean.getData());
                        initNewsBean();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.i("通知公告", "" + volleyError);
            }
        });
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    private void initNewsBean() {
        for (int i = 0; i < dataBeans.size(); i++) {
            LinearLayout moreView = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.item_home_upmarqueeview, null);
            TextView tv = moreView.findViewById(R.id.item_home_upmarqueeview_textView);
            tv.setText(dataBeans.get(i).getNoticeTitle());
            views.add(moreView);
        }
        upMarqueeView.setViews(views);
    }

    private void initWeather() {
        StringRequest request = new StringRequest(Request.Method.GET, Constants.tianqiapi, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("天气预报", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String wea = json.getString("wea");
                    String wea_img = json.getString("wea_img");
                    weatherTextView.setText(wea);
                    switch (wea_img) {
                        case "xue":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon1);
                            break;
                        case "lei":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon2);
                            break;
                        case "shachen":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon3);
                            break;
                        case "wu":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon4);
                            break;
                        case "bingbao":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon5);
                            break;
                        case "yun":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon6);
                            break;
                        case "yu":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon7);
                            break;
                        case "yin":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon8);
                            break;
                        case "qing":
                            weatherIcon.setImageResource(R.mipmap.fragment_home_weather_icon9);
                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e("天气预报", "" + volleyError);
            }
        });
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        linearLayoutManager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new HomeAdapter(getActivity(), dataBeans1);
        recyclerView.setAdapter(adapter);

        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
        linearLayoutManager1.setSmoothScrollbarEnabled(true);
        linearLayoutManager1.setAutoMeasureEnabled(true);
        recyclerView1.setLayoutManager(linearLayoutManager1);
        recyclerView1.setHasFixedSize(true);
        recyclerView1.setNestedScrollingEnabled(false);
        adapter1 = new HomeAdapter1(getActivity(), listBeans);
        recyclerView1.setAdapter(adapter1);

        initDialog();
        initDialog1();
        initDialog2();
        initDialog3();
    }

    private void initDialog3() {
        builder3 = new CustomDialog.Builder(getActivity())
                .cancelTouchout(false)
                .view(R.layout.dialog_me_client)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y360)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_me_client_btn1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog3.dismiss();
                    }
                })
                .addViewOnclick(R.id.dialog_me_client_btn2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog3.dismiss();
                        startActivity(new Intent(getActivity(), HomePayActivity.class));
                    }
                });

        dialog3 = builder3.build();
        TextView textView = builder3.getView(R.id.dialog_me_client_btn2);
        textView.setText("去缴纳");
    }

    private void initDialog2() {
        builder2 = new CustomDialog.Builder(getActivity())
                .cancelTouchout(false)
                .view(R.layout.dialog_zxing2)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y420)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_zxing2_textView1, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        powerChangeMark = "0";
                        sweepMark = "0";
                        batteryVoltage = "";
                        battry = "";
                        dialog2.dismiss();
                        initZxing();
                    }
                })
                .addViewOnclick(R.id.dialog_zxing2_textView2, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        powerChangeMark = "1";
                        dialog2.dismiss();
                        dialog1.show();
                    }
                })
                .addViewOnclick(R.id.dialog_zxing2_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog2.dismiss();
                    }
                });
        dialog2 = builder2.build();
    }

    private void initDialog1() {
        builder1 = new CustomDialog.Builder(getActivity())
                .cancelTouchout(false)
                .view(R.layout.dialog_zxing1)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y420)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_zxing1_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog1.dismiss();
                    }
                });
        dialog1 = builder1.build();

        RecyclerView DialogRecyclerView = builder1.getView(R.id.dialog_zxing1_rcyclerView);
        DialogRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        dialogAdapter = new DialogAdapter(getActivity(), strings);
        DialogRecyclerView.setAdapter(dialogAdapter);

        dialogAdapter.setOnItemClickListener(new DialogAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                sweepMark = "0";
                batteryVoltage = "";
                battry = strings.get(position);
                powerChangeMark = "1";
                dialog1.dismiss();
                initZxing();
            }
        });
    }

    private void initDialog() {
        builder = new CustomDialog.Builder(getActivity())
                .cancelTouchout(false)
                .view(R.layout.dialog_zxing)
                .widthDimenRes(R.dimen.x520)
                .heightDimenRes(R.dimen.y400)
                .style(R.style.Dialog)
                .addViewOnclick(R.id.dialog_zxing_48, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sweepMark = "0";
                        batteryVoltage = "48";
                        battry = "";
                        powerChangeMark = "0";
                        dialog.dismiss();
                        initZxing();
                    }
                })
                .addViewOnclick(R.id.dialog_zxing_60, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        sweepMark = "0";
                        batteryVoltage = "60";
                        battry = "";
                        powerChangeMark = "0";
                        dialog.dismiss();
                        initZxing();
                    }
                })
                .addViewOnclick(R.id.dialog_zxing_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
        dialog = builder.build();
    }

    @OnClick({R.id.fragment_home_realName, R.id.fragment_home_weather, R.id.fragment_home_news, R.id.fragment_home_zxing,
            R.id.fragment_home_type1, R.id.fragment_home_type2, R.id.fragment_home_type3, R.id.fragment_home_type4, R.id.fragment_home_type5,
            R.id.fragment_home_icon, R.id.fragment_home_more1, R.id.fragment_home_more2, R.id.fragment_home_type6, R.id.fragment_home_type7,
            R.id.fragment_home_type8, R.id.fragment_home_type9, R.id.fragment_home_type10})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.fragment_home_realName:
                if (!BcUtils.getUserInfo().getUserState().equals("2")) {
                    realName.setVisibility(View.GONE);
                    return;
                }
                intent = new Intent(getActivity(), NameRenzhengActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_weather:
                break;
            case R.id.fragment_home_news:
                intent = new Intent(getActivity(), HomeNewsActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_zxing:
                Tools.toast("待开发，敬请期待！");
                if (true)
                    return;
                if (BcUtils.getUserInfo().getUserState().equals("2")) {
                    Toast.makeText(getActivity(), "请先实名认证！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (BcUtils.getUserInfo().getUserDeposit().equals("0") || BcUtils.getUserInfo().getUserDeposit().equals("")) {
                    TextView textView = builder3.getView(R.id.dialog_me_client_textView);
                    textView.setText("请先缴纳电池押金！");
                    dialog3.show();
//                    Toast.makeText(getActivity(), "请先缴纳电池押金！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (BcUtils.getUserInfo().getUserRent().equals("0") || BcUtils.getUserInfo().getUserRent().equals("")) {
                    TextView textView = builder3.getView(R.id.dialog_me_client_textView);
                    textView.setText("请先缴纳电池租金！");
                    dialog3.show();
//                    Toast.makeText(getActivity(), "请先缴纳电池租金！", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (BcUtils.getUserInfo().getUserDueTime().equals("")) {
                    TextView textView = builder3.getView(R.id.dialog_me_client_textView);
                    textView.setText("您的电池已到期！");
                    dialog3.show();
//                    Toast.makeText(getActivity(), "您的电池已到期！", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (compare_date(BcUtils.getUserInfo().getUserDueTime(), getCurrentTime()) != 1) {
                        TextView textView = builder3.getView(R.id.dialog_me_client_textView);
                        textView.setText("您的电池已到期！");
                        dialog3.show();
//                        Toast.makeText(getActivity(), "您的电池已到期！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                if (BcUtils.getUserInfo().getUserBindingNum().equals("") || BcUtils.getUserInfo().getUserBindingNum().equals("0")) {
                    Toast.makeText(getActivity(), "您没有电池可绑定！", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (Integer.parseInt(BcUtils.getUserInfo().getUserBindingNum()) == 1) {
                    if (BcUtils.getUserInfo().getUserBindingNumEd().equals("") || BcUtils.getUserInfo().getUserBindingNumEd().equals("0")) {
                        dialog.show();
                    } else {
                        sweepMark = "0";
                        batteryVoltage = "";
                        powerChangeMark = "";
                        battry = "";
                        initZxing();
                    }
                } else if (Integer.parseInt(BcUtils.getUserInfo().getUserBindingNum()) > 1) {
                    if (BcUtils.getUserInfo().getUserBindingNum().equals(BcUtils.getUserInfo().getUserBindingNumEd())) {
                        sweepMark = "0";
                        batteryVoltage = "";
                        strings.clear();

                        if (dialogAdapter != null)
                            dialogAdapter.notifyDataSetChanged();
                        dialog1.show();
                    } else {
                        if (BcUtils.getUserInfo().getUserBindingNumEd().equals("") || BcUtils.getUserInfo().getUserBindingNumEd().equals("0")) {
                            dialog.show();
                        } else {
                            dialog2.show();
                        }
                    }
                }
                break;
            case R.id.fragment_home_type1:
                intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("siteType", "1");
                startActivity(intent);
                break;
            case R.id.fragment_home_type2:
                if (userStart != 1) {
                    Tools.toast("请先实名认证！");
                    return;
                }
                intent = new Intent(getActivity(), SharedCarActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type3:
                Tools.toast("待开发，敬请期待！");
                if (true)
                    return;
                if (userStart != 1) {
                    Tools.toast("请先实名认证！");
                    return;
                }
                intent = new Intent(getActivity(), MapActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type4:
                Tools.toast("待开发，敬请期待！");
                if (true)
                    return;
                break;
            case R.id.fragment_home_type5:
//                intent = new Intent(getActivity(), MyDingdanActity.class);
                intent = new Intent(getActivity(), OrderActity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_icon:
                break;
            case R.id.fragment_home_more1:
                intent = new Intent(getActivity(), HomePopularActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_more2:
                intent = new Intent(getActivity(), HomeIndulgenceActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type6:
                intent = new Intent(getActivity(), HomePayActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type7:
                Tools.toast("待开发，敬请期待！");
                if (true)
                    return;
                if (userStart != 1) {
                    Tools.toast("请先实名认证！");
                    return;
                }
                intent = new Intent(getActivity(), MapActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type8:
                if (userStart != 1) {
                    Tools.toast("请先实名认证！");
                    return;
                }
                intent = new Intent(getActivity(), SharedCarActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_home_type9:
                Tools.toast("待开发，敬请期待！");
                break;
            case R.id.fragment_home_type10:
                Tools.toast("待开发，敬请期待！");
                break;
        }
    }

    private void initZxing() {
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        ZxingConfig config = new ZxingConfig();
        config.setShowAlbum(false);
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        startActivityForResult(intent, Constants.ZXINGE_MAIN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.ZXINGE_MAIN && resultCode == getActivity().RESULT_OK) {
            String content = data.getStringExtra(Constant.CODED_CONTENT);
            try {
                String code = new JSONObject(content).getString("code");
                initZxingData(code);
            } catch (JSONException e) {
                e.printStackTrace();
                initZxingData(content);
            }
//            机柜码 CDZD08KXHD190401298
//            电池码 {"code":"BT206002008KXZL190619019","chk":"66be2dd98a6e3e49c4cdd5a08a027bfd"}
        }
    }

    private void initZxingData(String content) {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.sweepcode, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("扫码功能", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {

                    } else {
                        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("content", content);
                    json_url.put("sweepMark", sweepMark);
                    json_url.put("batteryVoltage", batteryVoltage);
                    json_url.put("userBindingNum", BcUtils.getUserInfo().getUserBindingNum());
                    json_url.put("battry", battry);
                    json_url.put("powerChangeMark", powerChangeMark);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(getActivity()).addToRequestQueue(request);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (userStart != 2) {
            realName.setVisibility(View.GONE);
            return;
        }
        if (BcUtils.getUserInfo().getUserState().equals("2")) {
            realName.setVisibility(View.VISIBLE);
        } else {
            realName.setVisibility(View.GONE);
        }
    }

    private int compare_date(String DATE1, String DATE2) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    private String getCurrentTime() {
        long currentTime = System.currentTimeMillis();
        String timeNow = new SimpleDateFormat("yyyy-MM-dd").format(currentTime);
        return timeNow;
    }

}
